﻿#If Not NETFX_CORE Then

Imports System.IO

Namespace IO

    Public Class FileSystemGameDirectory
        Inherits GameDirectory

        Private Property PhysicalDirectoryInfo() As DirectoryInfo

        ''' <summary>
        ''' Create a new FileSystemGameDirectory object with full file system path.
        ''' </summary>
        ''' <param name="path">
        ''' Full path string.
        ''' </param>
        ''' <remarks></remarks>
        Public Sub New(ByVal path As String)
            Me.PhysicalPath = path
            Me.PhysicalDirectoryInfo = New DirectoryInfo(path)
        End Sub

        Public Overrides Function CreateDirectory(relativePath As String) As Boolean
            Try
                Dim di As New DirectoryInfo(Path.Combine(Me.PhysicalPath, relativePath))
                di.Create()
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Overrides Function DeleteDirectory(relativePath As String, Optional ByVal recursive As Boolean = False) As Boolean
            Dim di As New DirectoryInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Try
                di.Delete(recursive)
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Overrides Function DeleteFile(relativePath As String) As Boolean
            Dim fi As New FileInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Try
                fi.Delete()
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function

        Public Overrides Function DirectoryExists(relativePath As String) As Boolean
            Dim di As New DirectoryInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Return di.Exists
        End Function

        Public Overrides Function FileExists(relativePath As String) As Boolean
            Dim fi As New FileInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Return fi.Exists
        End Function

        Public Overrides Function GetDirectories() As List(Of String)
            Return (From item In Me.PhysicalDirectoryInfo.GetDirectories() Select item.Name).ToList()
        End Function

        Public Overloads Overrides Function GetDirectories(relativePath As String) As List(Of String)
            Dim di As New DirectoryInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Return (From item In di.GetDirectories() Select item.Name).ToList()
        End Function

        Public Overrides Function GetFiles() As List(Of String)
            Return (From item In Me.PhysicalDirectoryInfo.GetFiles() Select item.Name).ToList()
        End Function

        Public Overloads Overrides Function GetFiles(relativePath As String) As List(Of String)
            Dim di As New DirectoryInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Return (From item In di.GetFiles() Select item.Name).ToList()
        End Function

        Public Overrides Function OpenFile(relativePath As String, mode As FileMode, Optional ByVal fileAccess As FileAccess = System.IO.FileAccess.ReadWrite) As Stream
            Dim fi As New FileInfo(Path.Combine(Me.PhysicalPath, relativePath))
            Return fi.Open(mode, fileAccess, FileShare.Read)
        End Function

    End Class

End Namespace

#End If